<?php

/**
 * SubjectController
 * 
 * @author Radovan Dvorsky
 * @version 20100514
 */

require_once 'Zend/Controller/Action.php';

/**
 * TODO komplet prerobit
 */
class SubjectController extends Zend_Controller_Action {
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		
		$subjectModel = new Db_Subject();		
		
		$cache = Zend_Registry::get('cache');
		
		
		if(!($subjects = $cache->load('subjects'))){
			$order = $subjectModel->select()->order('name');		
			$subjects = $subjectModel->fetchAll($order);
			$cache->save($subjects,'subjects',array('subject'));				
		}
		
		if(!($questionsCount = $cache->load('subjecsQuestionsCount'))){				
			$questionModel = new Db_Question();
			foreach($subjects as $subject){
				$questionsCount[$subject->sid] = $questionModel->getQuestionsCount($subject->sid);
			}
			$cache->save($questionsCount,'subjecsQuestionsCount',array('subject'));
		}

		$this->view->questionsCount = $questionsCount;
		$this->view->subjects = $subjects;		
		
				
	}
	
	public function addAction(){
		
		$form = new Form_Subject_Add();
		$this->view->form = $form;
		
		if( $this->getRequest()->isPost() ){		
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				try{
					
					$subjectModel = new Db_Subject();
					$data = array(
						'name' => $form->getValue('subjectName')
					);			
					
					$subjectModel->insert($data);
					
					$cache = Zend_Registry::get('cache');
					$cache->remove('subjects');
					$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('subject'));
					$this->_redirect('/subject/');
					
				} catch( Zend_Exception $e){
					$this->view->message = 'Predmet sa neporadilo vložiť';
					$log = Zend_Registry::get('log');    		
    				$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);					
				}				
			}
		}
	}
	
	/**
	 * @deprecated Presunute do question controlleru
	 */
	public function editAction(){		
		
		$sid = $this->getRequest()->getParam('sid');		
		$form = new Form_Subject_Edit();		
		$cache = Zend_Registry::get('cache');		
		
		if(!($subject = $cache->load('subject' . $sid))){			
			$subjectModel = new Db_Subject();
			$select = $subjectModel->select(Zend_Db_Table::SELECT_WITH_FROM_PART);		
			$select->setIntegrityCheck(false)
			->joinLeft('question','question.sid = subject.sid')
			->where('subject.sid = ?', $sid)
			->order('qn');			
			$subject = $subjectModel->fetchAll($select);			 
			$cache->save($subject);
		}
		
		
		$subjectName = $subject->toArray();
		$form->getElement('name')->setValue($subjectName[0]['name']);
		$form->getElement('sid')->setValue($sid);
		
		
		$this->view->form = $form;
		$this->view->subject = $subject;
	}
	
	/**
	 * @deprecated Uz sa nepouziva, vsetko sa presuva do question controlleru
	 */
	public function addquestionAction(){
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->layout()->disableLayout();
		
		$sid = (int)$this->getRequest()->getParam('sid');
		$text = $this->getRequest()->getParam('text');		
		
		try{
			$questionModel = new Db_Question();			
			
			$select = $questionModel->select();
			$select->from('question',array('COUNT(qid) as count'))
			->where('sid = ?',$sid);			
			$questions = $questionModel->fetchAll($select);
			
			$lastOrder = $questions->current()->count++;
			
			$row = $questionModel->createRow();
			$row->sid = $sid;
			$row->text = $text;
			$row->qn = $lastOrder;
			$row->save();

			$cache = Zend_Registry::get('cache');
			$cache->remove('subject' . $sid);
			
			/**
			 * TODO prerobit tak aby fungovalo bez nutnosti tahat data z db
			 * pouzit premennu lastorder
			 */
			$where = $questionModel->getAdapter()->quoteInto('sid = ?',$sid)
				   . $questionModel->getAdapter()->quoteInto(' AND qn = ?',$lastOrder);
			
			$lastQuestion = $questionModel->fetchAll($where)->current();			
			
			$json = Zend_Json::encode(array(
				'qid' => $lastQuestion->qid,
				'sid' => $lastQuestion->sid,
				'qn' => ++$lastQuestion->qn,
				'text' => $lastQuestion->text	
			));
			
			echo $json;
			
		}catch(Zend_Db_Exception $e){
			echo Zend_Json::encode(array('error' => true));			
			$log = Zend_Registry::get('log');    		
			$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
		}
		
	}
	

	public function deleteAction(){
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->layout()->disableLayout();
		
		$sidPost = $this->getRequest()->getParam('id');
		
		if(!empty($sidPost)){
			$sids = explode(',',$sidPost);
			try{				
				$subjectModel = new Db_Subject();
				$adapter = $subjectModel->getAdapter();
				$adapter->beginTransaction();				
				
				foreach($sids as $sid){					
					$subjectModel->delete($adapter->quoteInto('sid = ?',$sid));
				}
				
				$cache = Zend_Registry::get('cache');
				$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('subject'));
				$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('question'));
				$cache->remove('subjects');
				$adapter->commit();
				echo '<p class="success">Predmety odstránené</p>';
				
			} catch (Zend_Exception $e){
				echo '<p class="error">Predmety sa nepodarilo odstrániť</p>';
				$adapter->rollBack();
				$log = Zend_Registry::get('log');    		
	    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);	
			}
		}
	}
}